#!/usr/bin/env bash
# httpd

if [[ -z "${c_httpd}" ]]; then
    f_info 'httpd: not install';
    echo > /dev/null;  return;
fi;

f_wait 'httpd: install & config';

# install  忽略服务错误,与nginx端口冲突
f_inst 'httpd' "apache2 apache2-dev ${c_httpd_mod}";
own='www:www';  dir='/home/www';

# host
if [[ ! -d "${dir}/host/aac-default" ]]; then
    mkdir -p "${dir}/host" || f_fail 'httpd: mkdir host';
    f_copy "${RUN}/file/host" "${dir}/host/aac-default";
    f_mode "${dir}/host" '-' '700' "${own}";
fi;

# logs
mkdir -p "${dir}/logs" || f_fail 'httpd: mkdir logs'
f_mode "${dir}/logs" '700' '-' "${own}";

# conf
cfg="${dir}/conf";  apc="${cfg}/@apc"; mod="${apc}/mods";
if [[ ! -d "${mod}"  ]]; then
    if [[ ! -d "${cfg}" ]]; then not="${cfg}";
    elif [[ ! -d "${apc}" ]]; then not="${apc}";
    else not="${mod}"; fi;
    mkdir -p "${mod}" || f_fail 'httpd: mkdir conf';
    f_mode "${not}" '755' '-' "${own}";
fi;
src="${RUN}/conf/httpd";  f_link '/etc/apache2' "${apc}/.raw";
if [[ ! -f "${cfg}/apc.conf" ]]; then
    f_copy "${src}/aac.conf" "${cfg}/apc.conf" '755' '-' "${own}";
fi;
for v in $(ls "${src}/mods/"*".conf"); do
    t="${mod}/$(basename $v)";  [[ -e "${t}" ]] && continue;
    f_copy "${v}" "${t}" '755' '-' 'www:www';
done;
if [[ ! -f "${apc}/aac.default.conf" ]]; then
    f_copy "${src}/default.conf" "${apc}/aac.default.conf" '755' '-' "${own}";
fi;
f_bkup '/etc/apache2/apache2.conf';
f_link "${cfg}/apc.conf" '/etc/apache2/apache2.conf';

# auto & start
f_auto 'httpd' 'apache2' 1;
systemctl restart apache2 || f_fail 'httpd: restart srv';

# del user
if [[ -n "$(id -u apache 2>/dev/null)" ]]; then
    userdel apache || f_fail 'httpd: user delete';
fi;

echo > /dev/null;
